package com.example.service;

import com.example.entity.Account;
import com.example.entity.CourseCatalog;
import com.example.mapper.CourseCatalogMapper;
import com.example.utils.TokenUtils;
import org.springframework.stereotype.Service; // 用于标注服务层组件
import org.springframework.beans.factory.annotation.Autowired; // 用于自动装配
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CourseCatalogService {
    @Resource
    private CourseCatalogMapper courseCatalogMapper;

    public List<CourseCatalog> findAllByCourseId(Long courseId) {
        return courseCatalogMapper.selectAllByCourseId(courseId);
    }

    public CourseCatalog findById(Long id) {
        return courseCatalogMapper.selectById(id);
    }




    @Transactional
    public void addCourseCatalog(CourseCatalog courseCatalog) {
        Integer maxId = courseCatalogMapper.findMaxId();
        if (maxId == null) {
            courseCatalog.setId(1L); // 如果表为空，从1开始
        } else {
            courseCatalog.setId((long) (maxId + 1)); // 否则，使用最大ID加1
        }
        courseCatalogMapper.insertCourseCatalog(courseCatalog);
    }



    @Transactional
    public void updateCourseCatalog(Long id, CourseCatalog courseCatalog) {
        if (courseCatalog == null || !id.equals(courseCatalog.getId())) {
            throw new IllegalArgumentException("ID不匹配或请求体中缺少CourseCatalog对象");
        }
        courseCatalogMapper.updateByPrimaryKey(courseCatalog);
    }

    @Transactional
    public void deleteCourseCatalog(Long id) {
        courseCatalogMapper.deleteByPrimaryKey(id); // 调用数据访问层的删除方法
    }
}